//
//  BKCMBuyCarMainCell.m
//  bestkeep
//
//  Created by 毛韶谦 on 2017/1/16.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKCMBuyCarMainCell.h"
#import "BKSCCommon.h"
#import "BKAnimatedAlert.h"

@interface BKCMBuyCarMainCell ()<CellDataViewDelegate>

@property (nonatomic,strong) BKCMRecommendGoodsModel* footerModel;
@property (nonatomic,strong) BKCMGoodsDetailModel * buyCarModel;
@property (nonatomic,strong) UIView *moreView;
@property (nonatomic,strong) UIImageView *saveIcon;
@property (nonatomic,strong) UIImageView *deleteIcon;

@end

@implementation BKCMBuyCarMainCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        
        self.mainView = [[UIView alloc] init];
        self.mainView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.mainView];
        
        self.backgroundColor = COLOR_ORDER_DETAIL_Line;
        
        //圈圈
        _button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button1 setBackgroundColor:[UIColor whiteColor]];
        [_button1 setBackgroundImage:[UIImage imageNamed:@"BKSC_select_image"] forState:UIControlStateSelected];
        [_button1 setBackgroundImage:[UIImage imageNamed:@"BKSC_unselect_image"] forState:UIControlStateNormal];
        _button1.layer.cornerRadius = 9.5f;//8--12
        _button1.layer.masksToBounds = YES;
        [_button1 addTarget:self action:@selector(selectedButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self.mainView addSubview:_button1];
        
        //big按钮；
        _bigButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_bigButton1 setBackgroundColor:[UIColor clearColor]];
        [_bigButton1 addTarget:self action:@selector(selectedButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.mainView addSubview:_bigButton1];

        _imageView1 = [[UIImageView alloc] initWithFrame:CGRectZero];
        //        _imageView1.layer.borderWidth = 0.5;
        //        _imageView1.layer.borderColor = [UIColor fromHexValue:0xdadada].CGColor;
        _imageView1.contentMode = UIViewContentModeScaleAspectFit;
        [self.mainView addSubview:_imageView1];
        
        _detailView = [[BKCMCellDetailView alloc] init];
        [self.mainView addSubview:_detailView];
        
        _addView = [[BKCMCellAmountView alloc] init];
        [self.mainView addSubview:_addView];
        _addView.dataViewDelegate = self;
        
        [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top);
            make.left.mas_equalTo(self.contentView.mas_left);
            make.right.mas_equalTo(self.contentView.mas_right);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_offset(-0.5f);
        }];
        
        [_button1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mainView.mas_left).offset(ApplicationBackGauge);
            
            make.width.equalTo(@(19));//18--24
            make.height.equalTo(@(19));//28--24
            
            make.centerY.mas_equalTo(self.imageView1.mas_centerY);
            
        }];
        
        [_imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_button1.mas_right).offset(6.0f);
            make.size.mas_equalTo(CGSizeMake(100, 100));
            make.top.mas_equalTo(self.mainView.mas_top).offset(15.0f);
        }];
        
        _assistView = [[UIView alloc] init];
        _assistView.backgroundColor = [BKSCCommon fromHexValue:0xaaaaaa];
//        _assistView.alpha = 0.7;
        [self.mainView addSubview:_assistView];
        [_assistView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_imageView1.mas_left);
            make.right.mas_equalTo(_imageView1.mas_right);
            make.top.mas_equalTo(_imageView1.mas_top);
            make.bottom.mas_equalTo(_imageView1.mas_bottom);
        }];
        [self.mainView sendSubviewToBack:_assistView];
        
        _uselessTag = [UILabel new];
        _uselessTag.backgroundColor = [BKSCCommon fromHexValue:0xaaaaaa];
        _uselessTag.text = @"已失效";
        _uselessTag.textAlignment = NSTextAlignmentCenter;
        _uselessTag.textColor = [UIColor whiteColor];
        _uselessTag.font = [UIFont boldSystemFontOfSize:14];
        [self.mainView addSubview:_uselessTag];
        [_uselessTag mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_imageView1.mas_left);
            make.right.mas_equalTo(_imageView1.mas_right);
            make.bottom.mas_equalTo(_imageView1.mas_bottom);
            make.height.mas_equalTo(25);
        }];
        _uselessTag.hidden = YES;
        [_bigButton1 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(self.contentView.mas_left);
            make.right.mas_equalTo(self.imageView1.mas_left);
            make.bottom.mas_equalTo(_imageView1.mas_bottom);
            make.top.mas_equalTo(_imageView1.mas_top);
        }];
        
        [_detailView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_imageView1.mas_right).offset(12.0f);
            make.right.mas_equalTo(self.mainView.mas_right).offset(10-ApplicationBackGauge);
            make.top.mas_equalTo(self.mainView.mas_top).offset(14.5);
            make.bottom.mas_equalTo(self.mainView.mas_bottom).offset(-15);
        }];
        
        [_addView mas_makeConstraints:^(MASConstraintMaker *make) {
            //2016年05月27日22:48:24
            make.right.mas_equalTo(self.mainView.mas_right).offset(10-ApplicationBackGauge);
            make.bottom.mas_equalTo(_detailView.mas_bottom).offset(-1);
            make.width.mas_equalTo(@(95));//66---90
            make.height.mas_equalTo(@(50));//19--30
        }];
        
        _moreView = [[UIView alloc] init];
        _moreView.backgroundColor = COLOR_03;
        [self.contentView addSubview:_moreView];
        [_moreView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.contentView.mas_top);
            make.left.mas_equalTo(self.contentView.mas_right);
            make.bottom.mas_equalTo(self.contentView.mas_bottom);
            make.width.mas_equalTo(IPHONE_WIDTH);
        }];
        
        UIView *leftView = [[UIView alloc] init];
        leftView.backgroundColor = [UIColor whiteColor];
        [_moreView addSubview:leftView];
        [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_right).mas_offset(1);
            make.top.mas_equalTo(self.moreView.mas_top);
            make.height.mas_equalTo(self.mainView.mas_height);
            make.width.mas_equalTo(@66);
        }];
        UIView *rightView = [[UIView alloc] init];
        rightView.backgroundColor = [UIColor whiteColor];
        [_moreView addSubview:rightView];
        [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_right).mas_offset(68);
            make.top.mas_equalTo(self.contentView.mas_top);
            make.height.mas_equalTo(self.mainView.mas_height);
            make.width.mas_equalTo(@66);
        }];
        _deleteIcon = [[UIImageView alloc] init];
        _deleteIcon.userInteractionEnabled = YES;
        _deleteIcon.image = [UIImage imageNamed:@"BKShoppingCarDelete"];
        [_deleteIcon setContentMode:UIViewContentModeScaleAspectFit];
        [_moreView addSubview:_deleteIcon];
        [_deleteIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(rightView.mas_centerX);
            make.centerY.mas_equalTo(self.contentView.mas_centerY).mas_offset(-15);
            make.height.mas_equalTo(@17);
            make.width.mas_equalTo(@19);
        }];
        _saveIcon = [[UIImageView alloc] init];
        _saveIcon.userInteractionEnabled = YES;
        _saveIcon.image = [UIImage imageNamed:@"BKSCShoppingCartCollect"];
        [_saveIcon setContentMode:UIViewContentModeScaleAspectFit];
        [_moreView addSubview:_saveIcon];
        [_saveIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(leftView.mas_centerX);
            make.centerY.mas_equalTo(self.moreView.mas_centerY).mas_offset(-15);
            make.height.mas_equalTo(17);
            make.width.mas_equalTo(19);
        }];
        
        UILabel *deleteLabel = [[UILabel alloc] init];
        [_moreView addSubview:deleteLabel];
        deleteLabel.text = BKSCLocalizedString(@"BKFootViewDelegate");
        deleteLabel.textColor = RGB(64, 64, 64);
        deleteLabel.font = [UIFont systemFontOfSize:14];
        [deleteLabel setTextAlignment:NSTextAlignmentCenter];
        [deleteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.deleteIcon.mas_bottom).mas_offset(5);
            make.centerX.mas_equalTo(self.deleteIcon.mas_centerX);
        }];
        UILabel *saveLabel = [[UILabel alloc] init];
        [_moreView addSubview:saveLabel];
        saveLabel.text = BKSCLocalizedString(@"BKSCCollectGoods");
        saveLabel.textColor = RGB(64, 64, 64);
        saveLabel.font = [UIFont systemFontOfSize:14];
        [saveLabel setTextAlignment:NSTextAlignmentCenter];
        [saveLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.saveIcon.mas_bottom).mas_offset(5);
            make.centerX.mas_equalTo(self.saveIcon.mas_centerX);
        }];
    }
    return self;
}

- (void)setTempStr:(NSString *)tempStr {
    
    if ([tempStr isEqualToString:@""]) {
        _button1.hidden = YES;
        _bigButton1.userInteractionEnabled = NO;
    }else {
        _button1.hidden = NO;
        _bigButton1.userInteractionEnabled = YES;
    }
    
}


-(void)updateCellWithModel:(BKCMGoodsDetailModel *)model{
    self.buyCarModel = model;
    if (model.isEdit) {
        [self.addView updateDataWithShoppingCarList:model];
    }
}

-(void)selectedButtonClick{
    
    _button1.selected = !_button1.selected;
    _bigButton1.selected = _button1.selected;
    self.buyCarModel.isCellBtnSelect = _button1.selected;
    if ([self.cellBtnDetegate respondsToSelector:@selector(updataCellButtonModel:tableViewCell:)]) {
        [self.cellBtnDetegate updataCellButtonModel:self.buyCarModel tableViewCell:self];
    }
}

-(void)cellDataView:(BKCMCellAmountView *)dataView addCount:(UITextField *)addTextField{
    
    if ([addTextField.text integerValue] >= 200) {
        BKAnimatedAlert *animatedAlert = [[BKAnimatedAlert alloc] init];
        [animatedAlert BKAlert_Style:BKAlertStyle_Waring detail:@"最多买200件哦!"];
        [self setModelAmountWithIntger:200];
    }else{
        [self addOrSubCountWithAdd:YES];
    }
}

-(void)cellDataView:(BKCMCellAmountView *)dataView subCount:(UITextField *)subTextField{
    [self addOrSubCountWithAdd:NO];
}
-(void)cellDataView:(BKCMCellAmountView *)dataView deletButtonClick:(UITextField *)deleteTextField{
    
}
-(void)cellDataView:(BKCMCellAmountView *)dataView updateButtonClick:(UITextField *)amountField{
    
    CGFloat amount = [amountField.text integerValue];
    if (amount > 200) {
        BKAnimatedAlert *animatedAlert = [[BKAnimatedAlert alloc] init];
        [animatedAlert BKAlert_Style:BKAlertStyle_Waring detail:@"最多买200件哦!"];
        //            [ShowMessage showMessage:@"最多只能买200件哦!"];
        [self setModelAmountWithIntger:200];
        
    }else if (amount == 0) {
        [self setModelAmountWithIntger:1];
    }else{
        [self setModelAmountWithIntger:amount];
    }
}

-(void)cellDataView:(BKCMCellAmountView *)dataView cancleButtonClick:(UITextField *)cancleField{
    
    [self setModelAmountWithIntger:[self getCount]];
}

-(void)addOrSubCountWithAdd:(BOOL)isAdd{
    
    if (isAdd) {
        
        [self setModelAmountWithIntger:[self getCount] + 1];
    }else{
        if ([self getCount ] == 1 || [self getCount] < 1) {
//            [Common showAlertViewWith:@"" message:@"物品不能为空"];
            [HUD showMessage:@"物品不能为空"];
            
        }else{
            [self setModelAmountWithIntger:[self getCount] - 1];
        }
    }
    [self.addView updateDataWithShoppingCarList:self.buyCarModel];
}
-(void)setModelAmountWithIntger:(NSInteger)count{
    
    NSString * countStr = [NSString stringWithFormat:@"%ld",(long)count];
    self.buyCarModel.amount = countStr;
    
    if ([self.cellBtnDetegate respondsToSelector:@selector(updataCellContentData:tableViewCell:)]) {
        [self.cellBtnDetegate updataCellContentData:self.buyCarModel tableViewCell:self];
    }
}

-(NSInteger)getCount{
    
    return [self.buyCarModel.amount integerValue];
}

-(void)dealloc{
    
}
//- (void)SectionFooterView:(BKCMFooterView*)sectionFooterView andModel:(BKCMRecommendGoodsModel*) footerModel {
//    
//    self.footerAccountView = sectionFooterView;
//    self.footerModel = footerModel;
//}

/**
 *  处理Cotroller中的事件
 *
 *  @param buyCarDetailCell 注册的cell
 *  @param shopList1        所需的数据
 */
- (void)dealBuyCarDetailCell:(BKCMBuyCarMainCell *)buyCarDetailCell superShopping:(BKCMRecommendGoodsModel *)shopList1 leftButton:(UIButton *)leftButton indexPath:(NSIndexPath *)indexPath{
    
    BKCMGoodsDetailModel* cellList = shopList1.shopList[indexPath.row];
    
    if (cellList.shopcart_flag) {
        buyCarDetailCell.uselessTag.hidden = YES;
        buyCarDetailCell.detailView.userInteractionEnabled = YES;
        buyCarDetailCell.assistView.hidden = YES;
        buyCarDetailCell.imageView1.alpha = 1;
        buyCarDetailCell.addView.tempStr = @"1";//调用，使加减按钮变色
        buyCarDetailCell.tempStr = @"1"; //处理选择按钮事件；
        
    }else if (leftButton.selected){
        buyCarDetailCell.uselessTag.hidden = NO;
        buyCarDetailCell.detailView.userInteractionEnabled = NO;
        buyCarDetailCell.assistView.hidden = NO;
        buyCarDetailCell.imageView1.alpha = 0.5;
        buyCarDetailCell.addView.tempStr = @"";//调用，使加减按钮变色
        buyCarDetailCell.tempStr = @"1"; //处理选择按钮事件；
    }else {
        buyCarDetailCell.uselessTag.hidden = NO;
        buyCarDetailCell.detailView.userInteractionEnabled = NO;
        buyCarDetailCell.assistView.hidden = NO;
        buyCarDetailCell.imageView1.alpha = 0.5;
        buyCarDetailCell.addView.tempStr = @"";//调用，使加减按钮变色
        buyCarDetailCell.tempStr = @""; //处理选择按钮事件；
    }
    
    [buyCarDetailCell.imageView1 sd_setImageWithURL:[NSURL URLWithString:cellList.goods_img] placeholderImage:[UIImage imageNamed:@"BKSCpic_newGoods"]];   //物品
    buyCarDetailCell.addView.amount = cellList.amount; //加减号数量
    
    buyCarDetailCell.detailView.cellDetail = shopList1.shopList[indexPath.row];   //  商品详情页面模型
    
    [buyCarDetailCell updateCellWithModel:cellList];
    
    buyCarDetailCell.button1.selected = cellList.isCellBtnSelect;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
